﻿
using System;
using System.Collections.Generic;
using sharpxbot.Plugins.BibleSearch.Data;
using sharpxbot.Plugins.BibleSearch.FuzzySearch;
using sharpxbot.Plugins.BibleSearch.Utils;
namespace sharpxbot.Plugins.BibleSearch
{
    public class FuzzySearchAdapter
    {
        public FuzzySearchAdapter(BibleCache cache)
        {
            if (cache == null)
                throw new NullReferenceException("Illegal null-reference cache");

            this.cache = cache;
            this.util = new VerseUtil(cache);
        }

        public List<Verse> Convert(List<DocumentContainer> documents)
        {
            if (documents == null)
                return new List<Verse>();

            List<Verse> result = new List<Verse>();

            try
            {
                foreach (DocumentContainer d in documents)
                {
                    int verseID = Int32.Parse(d.Document.Get("id"));
                    int verseIndex = Int32.Parse(d.Document.Get("verseIndex"));
                    int chapterID = Int32.Parse(d.Document.Get("chapterID"));
                    string verseText = d.Document.Get("text");

                    Verse v = new Verse()
                    {
                        ChapterID = chapterID,
                        Text = verseText,
                        VerseIndex = verseIndex,
                        Relevantity = (int)(d.Relevantity * 100)
                    };

                    VerseInfo info = util.GetVerseInfo(v);
                    if (info != null)
                    {
                        v.ChapterIndex = info.ChapterIndex;
                        v.BookNameAbbreviation = info.BookNameAbbreviation;

                        result.Add(v);
                    }
                }
            }
            catch { }

            return result;
        }

        BibleCache cache;
        VerseUtil util;
    }
}
